package com.decapre.zufangwang.service;

import com.decapre.zufangwang.entity.House;
import com.decapre.zufangwang.entity.dto.SearchParamDto;
import com.decapre.zufangwang.repository.HouseRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class HouseServiceImpl implements HouseService {

    private HouseRepository houseRepository;

    @Autowired
    public HouseServiceImpl(HouseRepository hr){
        this.houseRepository = hr;
    }

    @Override
    public Map<String,Object> getAllHouses() {
        List<House> houses = houseRepository.getAllHouses();
        Map<String,Object> data = new HashMap<>();
        data.put("pagination",houses.size()%3==0?houses.size()/3:houses.size()/3+1);
        data.put("houses",houses);
        return data;
    }



    @Override
    public Map<String, Object> getHousesByForm(SearchParamDto formData) {
        Map<String,Object> data = new HashMap<>();
        List<House> houses;
        List<Integer> housesIds = houseRepository.getHouseIdsByParams(
                formData.getDistricts(),
                formData.getBedroomCount(),
                formData.getLivingroomCount(),
                formData.getWashroomCount(),
                formData.getMinPrice(),
                formData.getMaxPrice()
        );
        if (!housesIds.isEmpty()) {
            houses = houseRepository.getHousesByHouseIds(housesIds);
        }
        else{
            houses = new ArrayList<>();
        }
        data.put("houses",houses);
        data.put("pagination", houses.size()%3==0?houses.size()/3:houses.size()/3+1);
        return data;
    }

//    @Override
//    public Map<String, Object> getHouseIdsByDistricts(List<Integer> districtIds) {
//        return null;
//    }




}
